package com.example.lemonhealth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemonhealth.dao.TCheckgroupCheckitemMapper;
import com.example.lemonhealth.dao.TCheckgroupMapper;
import com.example.lemonhealth.entity.TCheckgroup;
import com.example.lemonhealth.entity.TCheckgroupCheckitem;
import com.example.lemonhealth.entity.TCheckitem;
import com.example.lemonhealth.exception.BaseException;
import com.example.lemonhealth.service.CheckgroupService;
import com.example.lemonhealth.utils.PageResult;
import com.example.lemonhealth.utils.QueryPageBean;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: Lemon
 * @Description: 检查组管理相关接口
 * @Date: 2024/12/24 16:28
 */
@Service
public class CheckgroupServiceImpl implements CheckgroupService {

    @Resource
    private TCheckgroupMapper tCheckgroupMapper;

    @Resource
    private TCheckgroupCheckitemMapper tCheckgroupCheckitemMapper;

    /**
     * @Author Lemon
     * @Description 分页查询-检查组管理
     * @Date 2024/12/24 16:34
     * @Param [queryPageBean]
     * @Return com.baomidou.mybatisplus.core.metadata.IPage
     */
    @Override
    public IPage<TCheckgroup> findPage(QueryPageBean queryPageBean) {
        Page<TCheckgroup> page = new Page(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        LambdaQueryWrapper<TCheckgroup> productQuery = Wrappers.lambdaQuery();
        Page<TCheckgroup> tCheckgroupPage = tCheckgroupMapper.selectPage(page, productQuery);
        IPage<TCheckgroup> iTCheckgroupPagePage = new Page<>();
        BeanUtils.copyProperties(tCheckgroupPage, iTCheckgroupPagePage);
        List<TCheckgroup> productVoList = tCheckgroupPage.getRecords().stream().map(
                e -> product2ProductVo(e)
        ).collect(Collectors.toList());
        iTCheckgroupPagePage.setRecords(productVoList);
        iTCheckgroupPagePage.setTotal(tCheckgroupPage.getTotal());
        return iTCheckgroupPagePage;
    }

    /**
     * @Author Lemon
     * @Description 添加检查组
     * @Date 2024/12/24 17:57
     * @Param [checkitemIds, checkgroup]
     * @Return void
     */
    @Override
    public void addCheckgroup(String checkitemIds, TCheckgroup checkgroup) {
        tCheckgroupMapper.insert(checkgroup);
        TCheckgroup checkgroup1 = tCheckgroupMapper.selectOne(
                new LambdaQueryWrapper<TCheckgroup>()
                        .eq(TCheckgroup::getCode, checkgroup.getCode())
        );
        //添加中间表
        if (StringUtils.isNotEmpty(checkitemIds)) {
            String[] split = checkitemIds.split(",");
            for (String id : split) {
                TCheckgroupCheckitem tCheckgroupCheckitem = new TCheckgroupCheckitem();
                tCheckgroupCheckitem.setCheckgroupId(checkgroup1.getId());
                tCheckgroupCheckitem.setCheckitemId(Integer.valueOf(id));
                tCheckgroupCheckitemMapper.insert(tCheckgroupCheckitem);
            }
        }
    }

    /**
     * @Author Lemon
     * @Description 根据id查询检查组（数据回显）
     * @Date 2024/12/25 14:57
     * @Param [id]
     * @Return com.example.lemonhealth.entity.TCheckgroup
     */
    @Override
    public TCheckgroup findById(String id) {
        TCheckgroup tCheckgroup = tCheckgroupMapper.selectOne(
                new LambdaQueryWrapper<TCheckgroup>()
                        .eq(TCheckgroup::getId, id)
        );
        return tCheckgroup;
    }

    /**
     * @Author Lemon
     * @Description 根据checkgroupId查询检查项（数据回显）
     * @Date 2024/12/25 15:34
     * @Param [checkGroupId]
     * @Return java.util.List
     */
    @Override
    public List findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        List<Integer> checkitemList = tCheckgroupCheckitemMapper.findCheckItemIdsByCheckGroupId(checkGroupId);
        return checkitemList;
    }

    /**
     * @Author Lemon
     * @Description 修改检查组
     * @Date 2024/12/25 17:28
     * @Param [checkitemIds, checkgroup]
     * @Return void
     */
    @Override
    public void editCheckgroup(String checkitemIds, TCheckgroup checkgroup) {
        tCheckgroupMapper.updateById(checkgroup);
        //判断checkitemIds是否为空
        if (StringUtils.isNotEmpty(checkitemIds)) {
            //不为空，先删除原来的关联数据
            tCheckgroupCheckitemMapper.delete(
                    new LambdaQueryWrapper<TCheckgroupCheckitem>()
                            .eq(TCheckgroupCheckitem::getCheckgroupId, checkgroup.getId())
            );
            //重新添加关联表
            String[] split = checkitemIds.split(",");
            for (String id : split) {
                TCheckgroupCheckitem tCheckgroupCheckitem = new TCheckgroupCheckitem();
                tCheckgroupCheckitem.setCheckgroupId(checkgroup.getId());
                tCheckgroupCheckitem.setCheckitemId(Integer.valueOf(id));
                tCheckgroupCheckitemMapper.insert(tCheckgroupCheckitem);
            }
        }

    }

    private TCheckgroup product2ProductVo(TCheckgroup product) {
        TCheckgroup productVo = new TCheckgroup();
        BeanUtils.copyProperties(product, productVo);
        return productVo;
    }
}
